package com.yupfeg.test.simple

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.yupfeg.livedata.R
import com.yupfeg.test.base.viewModelDelegate

/**
 * 简单使用Activity
 * @author yuPFeG
 * @date 2021/09/19
 */
class SimpleEventActivity : AppCompatActivity(){

    private val mViewModel : SimpleEventViewModel by viewModelDelegate()
    private lateinit var tvEventContent : TextView
    private lateinit var tvLiveDataContent : TextView

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_simple_event)

        tvEventContent = findViewById(R.id.tv_simple_event_content)
        tvLiveDataContent = findViewById(R.id.tv_simple_normal_data)

        findViewById<Button>(R.id.btn_send_new_simple_event).setOnClickListener {
            mViewModel.sendSimpleEvent()
        }

        findViewById<Button>(R.id.btn_simple_test_date).setOnClickListener {
            mViewModel.getTestData()
        }

        //订阅单次执行事件
        mViewModel.simpleEvent.observe(this){event->
            tvEventContent.text = "事件内容：${event}"
            Toast.makeText(this,"接收到单次事件",Toast.LENGTH_SHORT).show()
        }

        mViewModel.normalLiveData.observe(this){data->
            Log.d("logger","接收到原始LiveData数据 : $data")
            tvLiveDataContent.text = "原始LiveData : $data"
        }
    }
}